package game.editer;

import hx.layout.AnchorLayout;
import hx.display.Box;
import hx.events.MouseEvent;
import game.editer.ui.CommonButton;
import motion.Actuate;
import hx.display.Quad;
import hx.layout.AnchorLayoutData;

/**
 * 基础的窗口功能，该窗口会自带一个模态层，无法触摸后面的内容
 */
class BaseView extends BasePanel {
	public var body:Box;

	public function new(title:String, width:Int, height:Int) {
		super(title);
		this.width = width;
		this.height = height;
		this.layoutData = AnchorLayoutData.center();
	}

	override function onInit() {
		super.onInit();
		var closeBtn = new CommonButton("UIAtlas:close");
		this.addChild(closeBtn);
		closeBtn.layoutData = AnchorLayoutData.topRight(8, 8);
		closeBtn.addEventListener(MouseEvent.CLICK, (e) -> {
			this.parent.removeChild(this);
		});

		body = new Box();
		this.addChild(body);
		this.layout = new AnchorLayout();
		body.layout = new AnchorLayout();
		body.layoutData = new AnchorLayoutData(35, 4, 4, 4);
	}

	var model:Quad;

	override function onAddToStage() {
		super.onAddToStage();
		model = new Quad(1, 1, 0x0);
		model.alpha = 0.;
		model.layoutData = AnchorLayoutData.fill();
		this.topView.addChildAt(model, this.topView.numChildren - 1);
		Actuate.tween(model, 0.5, {alpha: 0.5});
	}

	override function onRemoveToStage() {
		super.onRemoveToStage();
		if (model != null) {
			Actuate.tween(model, 0.5, {alpha: 0.}).onComplete(() -> {
				model.parent.removeChild(model);
				model = null;
			});
		}
	}
}
